package com.jf3q.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jf3q.common.service.EduCourseDescriptionService;
import com.jf3q.common.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf3q.common.domain.EduChapter;
import com.jf3q.common.domain.EduCourse;

import com.jf3q.common.domain.EduCourseDescription;
import com.jf3q.common.domain.EduVideo;
import com.jf3q.common.exception.JfException;
import com.jf3q.common.mapper.EduChapterMapper;
import com.jf3q.common.mapper.EduCourseMapper;
import com.jf3q.common.mapper.EduVideoMapper;
import com.jf3q.common.vo.CourseInfoVo;
import com.jf3q.common.vo.CourseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse>
    implements EduCourseService {
    //课程描述注入
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;
    @Autowired
    EduChapterMapper chapterMapper;

    @Autowired
    EduVideoMapper videoMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    EduCourseMapper eduCourseMapper;

    @Override
    public String saveCourse(CourseInfoVo courseInfoVo) {
        //1 向课程表添加课程基本信息
        //CourseInfoVo对象转换eduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        boolean flag = this.saveOrUpdate(eduCourse);
        if(flag == false) {
            //添加失败
            throw new JfException(20001,"添加课程信息失败");
        }
        //获取添加之后课程id
        String cid = eduCourse.getId();
        //2 向课程简介表添加课程简介
        //edu_course_description
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        //设置描述id就是课程id
        courseDescription.setId(cid);
        courseDescriptionService.saveOrUpdate(courseDescription);
        //新增课程的时候把最新数据放到redis中
        if(StringUtils.checkValNull(courseInfoVo.getId())){
            redisTemplate.opsForList().leftPush("newCourses",eduCourse );
            // 获取当前列表的长度
            long listSize = redisTemplate.opsForList().size("newCourses");
            // 如果列表长度超过3，截断列表，保留最新的3个元素
            if (listSize > 3) {
                redisTemplate.opsForList().trim("newCourses", 0, 2);
            }
        }else{
            // 更新List中的课程对象
            redisTemplate.opsForList().set("newCourses", indexOfNewCourses(courseInfoVo.getId()), eduCourse);
        }
        return cid;
    }

    private int indexOfNewCourses(String courseId) {
        // 获取文章列表
        List<EduCourse> courses = redisTemplate.opsForList().range("newCourses", 0, -1);
        // 找到文章的索引
        for (int i = 0; i < courses.size(); i++) {
            if (courses.get(i).getId().equals(courseId)) {
                return i;
            }
        }
        return -1; // 文章不在列表中
    }

    @Override
    public void del(String id) {
        LambdaQueryWrapper<EduChapter> chapterQueryWrapper= new LambdaQueryWrapper();
        Long chapterNum = chapterMapper.selectCount(chapterQueryWrapper.eq(EduChapter::getCourseId, id));
        if (chapterNum>0){
            throw new JfException(50000,"该课程下有章节，删除失败!");
        }
        LambdaQueryWrapper<EduVideo> videoQueryWrapper= new LambdaQueryWrapper();
        Long videoNum = videoMapper.selectCount(videoQueryWrapper.eq(EduVideo::getCourseId, id));
        if (videoNum>0){
            throw new JfException(50000,"该课程下有小节，删除失败!");
        }
        this.removeById(id);
        courseDescriptionService.removeById(id);
    }
    public EduCourse getOver(String cId) {
        return baseMapper.getOver(cId);
    }

    //课程详情查询
    @Override
    public CourseVo getCourseId(String courseId) {
        return eduCourseMapper.getCourse(courseId);
    }
}




